package com.weblog.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weblog.weblog.common.domain.dos.CategoryDO;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;


import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

/**
 * @author gc
 * @class_name CategoryMapper
 * @date 2023年09月20日 13:29
 */

public interface CategoryMapper extends BaseMapper<CategoryDO> {

    default Page<CategoryDO> selectPageList(long current , long size, String name, LocalDate startDate, LocalDate endDate){
        //分页对象
        Page<CategoryDO> page = new Page<>(current, size);

        //构建查询条件
        LambdaQueryWrapper<CategoryDO> wrapper = new LambdaQueryWrapper<>();

        wrapper
                .like(StringUtils.isNotBlank(name), CategoryDO::getName,name.trim())
                .ge(Objects.nonNull(startDate),CategoryDO::getCreateTime, startDate)
                .le(Objects.nonNull(endDate),CategoryDO::getCreateTime , endDate)
                .orderByDesc(CategoryDO::getCreateTime);

        return selectPage(page,wrapper);
    }



    /**
     * 根据用户名查询
     * @author gc
     * @date 2023/9/20 13:29
     * @return null
     */

    default CategoryDO selectByName(String categoryName){
        //构建查询条件
        LambdaUpdateWrapper<CategoryDO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(CategoryDO::getName , categoryName);

        //执行查询
        return selectOne(wrapper);
    }

    default List<CategoryDO> selectByLimit(Long limit){
        return selectList(Wrappers.<CategoryDO>lambdaQuery()
                .orderByDesc(CategoryDO::getArticlesTotal)
                .last(String.format("LIMIT %d", limit)));
    }
}
